﻿
/*
NAILSUPERSTORE ® COMPANY
 
[HISTORY]
Created By  	: NGUYEN VAN TY
Created Date	: 3/04/2010
Modified By     :
Modified Date   :
Note            :

*/
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using CommonTagTime;
using CommonTagTime.ModelObject;
using CommonTagTime.Business;

namespace CommonTagTime.DataAccess
{
    public class daoBanner
    {
        public daoBanner()
        { 
            
        }
        public bool DeActive(int BannerID)
        {

            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[1];
            try
            {
                para[0] = new SqlParameter(busBanner.Id, SqlDbType.Int);
                para[0].Value = BannerID;
                result = clsSQL.SPExecute("Banner_DeActive", para);
            }
            catch (Exception ex)
            {
                clsFile.WriteLog("daoBanner", "DeActive(int BannerID)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }
            if (result > 0)
                return true;
            return false;
        }
        public bool Active(int BannerID)
        {

            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[1];
            try
            {
                para[0] = new SqlParameter(busBanner.Id, SqlDbType.Int);
                para[0].Value = BannerID;
                result = clsSQL.SPExecute("Banner_Active", para);
            }
            catch (Exception ex)
            {
                clsFile.WriteLog("daoBanner", "Active(int BannerID)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }
            if (result > 0)
                return true;
            return false;
        }
        public bool OrderDown(int BannerID)
        {

            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[1];
            try
            {
                para[0] = new SqlParameter(busBanner.Id, SqlDbType.Int);
                para[0].Value = BannerID;
                result = clsSQL.SPExecute("Banner_OrderDown", para);
            }
            catch (Exception ex)
            {
                clsFile.WriteLog("daoBanner", "OrderDown(int BannerID)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }
            if (result > 0)
                return true;
            return false;
        }
        public bool OrderUp(int BannerID)
        {

            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[1];
            try
            {
                para[0] = new SqlParameter(busBanner.Id, SqlDbType.Int);
                para[0].Value = BannerID;
                result = clsSQL.SPExecute("Banner_OrderUp", para);
            }
            catch (Exception ex)
            {
                clsFile.WriteLog("daoBanner", "OrderUp(int BannerID)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }
            if (result > 0)
                return true;
            return false;
        }
        public bool Delete(int BannerID)
        {
            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[1];
            try
            {
                para[0] = new SqlParameter(busBanner.Id, SqlDbType.Int);
                para[0].Value = BannerID;
                result = clsSQL.SPExecute("Banner_Delete", para);
            }
            catch (Exception ex)
            {
                clsFile.WriteLog("daoBanner", "Delete(Banner objBanner)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }
            if (result > 0)
                return true;
            return false;
        }
        public bool Insert(Banner objBanner)
        {
            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[11];
            Banner item = new Banner();
            try
            {
                para[0] = new SqlParameter(busBanner.BannerName, SqlDbType.NVarChar);
                para[0].Value = clsConvert.StringToDB(objBanner.BannerName);
                para[1] = new SqlParameter(busBanner.FilePath, SqlDbType.VarChar);
                para[1].Value = clsConvert.StringToDB(objBanner.FilePath);
                para[2] = new SqlParameter(busBanner.Url, SqlDbType.VarChar);
                para[2].Value = clsConvert.StringToDB(objBanner.Url);
                para[3] = new SqlParameter(busBanner.StartingDate, SqlDbType.DateTime);
                para[3].Value = clsConvert.DateToDB(objBanner.StartingDate);
                para[4] = new SqlParameter(busBanner.EndingDate, SqlDbType.DateTime);
                para[4].Value = clsConvert.DateToDB(objBanner.EndingDate);
                para[5] = new SqlParameter(busBanner.IsActive, SqlDbType.Bit);
                para[5].Value = objBanner.IsActive;               
                para[6] = new SqlParameter(busBanner.Effect, SqlDbType.TinyInt);
                para[6].Value = clsConvert.ByteToDB(objBanner.Effect);               
                para[7] = new SqlParameter(busBanner.CreatedBy, SqlDbType.NVarChar);
                para[7].Value = clsConvert.StringToDB(objBanner.CreatedBy);
                para[8] = new SqlParameter(busBanner.CreatedDate, SqlDbType.DateTime);
                para[8].Value = objBanner.CreatedDate;
                para[9] = new SqlParameter(busBanner.UpdatedBy, SqlDbType.NVarChar);
                para[9].Value = clsConvert.StringToDB(objBanner.UpdatedBy);
                para[10] = new SqlParameter(busBanner.UpdatedDate, SqlDbType.DateTime);
                para[10].Value = objBanner.UpdatedDate;
                result = clsSQL.SPExecute("Banner_Insert", para);
            }
            catch (Exception ex)
            {
                item = null;
                clsFile.WriteLog("daoBanner", "Insert(Banner)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }
            return (result>=1);
        }
        public bool Update(Banner objBanner)
        {
            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[10];
            try
            {
                para[0] = new SqlParameter(busBanner.BannerName, SqlDbType.NVarChar);
                para[0].Value = clsConvert.StringToDB(objBanner.BannerName);
                para[1] = new SqlParameter(busBanner.FilePath, SqlDbType.VarChar);
                para[1].Value = clsConvert.StringToDB(objBanner.FilePath);
                para[2] = new SqlParameter(busBanner.Url, SqlDbType.VarChar);
                para[2].Value = clsConvert.StringToDB(objBanner.Url);
                para[3] = new SqlParameter(busBanner.StartingDate, SqlDbType.DateTime);
                para[3].Value = clsConvert.DateToDB(objBanner.StartingDate);
                para[4] = new SqlParameter(busBanner.EndingDate, SqlDbType.DateTime);
                para[4].Value = clsConvert.DateToDB(objBanner.EndingDate);
                para[5] = new SqlParameter(busBanner.IsActive, SqlDbType.Bit);
                para[5].Value = objBanner.IsActive;
                para[6] = new SqlParameter(busBanner.Effect, SqlDbType.TinyInt);
                para[6].Value = clsConvert.ByteToDB(objBanner.Effect);               
                para[7] = new SqlParameter(busBanner.UpdatedBy, SqlDbType.NVarChar);
                para[7].Value = clsConvert.StringToDB(objBanner.UpdatedBy);
                para[8] = new SqlParameter(busBanner.UpdatedDate, SqlDbType.DateTime);
                para[8].Value = objBanner.UpdatedDate;
                para[9] = new SqlParameter(busBanner.Id, SqlDbType.Int);
                para[9].Value = objBanner.Id;
                result = clsSQL.SPExecute("Banner_Update", para);
            }
            catch (Exception ex)
            {
                clsFile.WriteLog("daoBanner", "Update(Banner objBanner)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }
            return (result>=1);
        }       
        private Banner Load(IDataReader de)
        {
            Banner item = new Banner();
            item.CreatedBy = clsConvert.DbToString("CreatedBy", de);
            item.CreatedDate = clsConvert.DbToDateTime("CreatedDate", de);
            item.UpdatedBy = clsConvert.DbToString("UpdatedBy", de);
            item.UpdatedDate = clsConvert.DbToDateTime("UpdatedDate", de);
            item.IsActive = clsConvert.DbToBoolean("IsActive", de);
            item.SortOrder = clsConvert.DbToInt("SortOrder", de);
            item.BannerName = clsConvert.DbToString("BannerName", de);
            item.FilePath = clsConvert.DbToString("FilePath", de);
            item.Url = clsConvert.DbToString("Url", de);
            item.Id = clsConvert.DbToInt("Id", de);
            item.Effect = clsConvert.DbToByte("Effect", de);
            item.StartingDate = clsConvert.DbToDateTime("StartingDate", de);
            item.EndingDate = clsConvert.DbToDateTime("EndingDate", de);
            return item;
        }

        public BannerCollection List(Banner objBanner)
        {

            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[6];
            SqlDataReader de;
            BannerCollection items = new BannerCollection();
            try
            {
                para[0] = new SqlParameter(busBanner.Condition, SqlDbType.NVarChar);
                para[0].Value = objBanner.Condition;
                para[1] = new SqlParameter(busBanner.OrderBy, SqlDbType.NVarChar);
                para[1].Value = objBanner.OrderBy;
                para[2] = new SqlParameter(busBanner.OrderDirection, SqlDbType.NVarChar);
                para[2].Value = objBanner.OrderDirection;
                para[3] = new SqlParameter(busBanner.CurrentPage, SqlDbType.Int);
                para[3].Value = objBanner.CurrentPage;
                para[4] = new SqlParameter(busBanner.PageSize, SqlDbType.Int);
                para[4].Value = objBanner.PageSize;
                para[5] = new SqlParameter(busBanner.TotalRecords, SqlDbType.Int);
                para[5].Direction = ParameterDirection.Output;
                de = clsSQL.SPDataReader("Banner_List", para);
                while (de.Read())
                {
                    Banner item = Load(de);
                    items.Add(item);
                }
                de.Close();
                objBanner.TotalRecords = Convert.ToInt32(para[5].Value);
                de.Dispose();
            }
            catch (Exception e)
            {
                clsFile.WriteLog("daoBanner", "List(Banner Banner)", e.ToString());
            }
            finally
            {
                de = null;
                clsSQL.Dispose();
                clsSQL = null;
            }
            return items;
        }

        public Banner GetByID(int BannerID)
        {
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[1];
            SqlDataReader de;
            Banner item = new Banner();
            try
            {
                para[0] = new SqlParameter(busBanner.Id, SqlDbType.Int);
                para[0].Value = BannerID;
                using (de = clsSQL.SPDataReader("Banner_GetByID", para))
                {
                    while (de.Read())
                    {
                        item = Load(de);
                    }
                    de.Close();
                    de.Dispose();
                }
            }
            catch (Exception ex)
            {
                item = null;
                clsFile.WriteLog("daoBanner", " GetByID(int BannerID)", ex.ToString());
            }
            finally
            {
                de = null;
                clsSQL.Dispose();
                clsSQL = null;
            }
            return item;
        }
        public BannerCollection GetAllActive()
        {
            clsSQL clsSQL = new clsSQL();         
            SqlDataReader de;
            BannerCollection items = new BannerCollection();
            try
            {
                de = clsSQL.SPDataReader("Banner_GetAllActive");
                while (de.Read())
                {
                    Banner item = Load(de);
                    items.Add(item);
                }
                de.Close();                
                de.Dispose();
            }
            catch (Exception e)
            {
                clsFile.WriteLog("daoBanner", "GetAllActive()", e.ToString());
            }
            finally
            {
                de = null;
                clsSQL.Dispose();
                clsSQL = null;
            }
            return items;
        }
    }    
}

